#!/bin/bash
getTomcatPid ()
{
	Tomcat_pid=`ps -ef|grep -w Bootstrap|grep -v grep|awk '{printf $2}'`
	return $Tomcat_pid
}

getTomcatPid

# Getting parameters:
while getopts "w:c:h" OPT;do
case $OPT in
"w")warning=$OPTARG;;
"c")critical=$OPTARG;;
"h")help;;
esac
done

Old_Total=`su --shell=/bin/bash tomcat -c "jstat -gc $Tomcat_pid"|tail -1|awk '{print $7}'`

Old_Used=`su --shell=/bin/bash tomcat -c "jstat -gc $Tomcat_pid"|tail -1|awk '{print $8}'`

Old_unused=$(echo "scale=2;($Old_Total - $Old_Used)/$Old_Total" | bc)

Old_unused_percentage=$(echo "$Old_unused*100" | bc)

if [[ $(echo "$Old_unused < $critical"|bc) -eq 1 ]];then
	msg="CRITICAL"
	status=2
else if [[ $(echo "$Old_unused < $warning"|bc) -eq 1 ]];then
	msg="WARNING"
	status=1
else
	msg="OK"
	status=0
	fi
fi

echo "$msg - Old_Unused Memory Ratio $Old_unused_percentage%"

exit $status